//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
// Portions Copyright (c) MiTAC Corp. All rights reserved.
//------------------------------------------------------------------------------
//
//  File:  menu.c
//  Programmer: Visual.Wei
//  Date: 2007-3-28
//
#include <windows.h>
#include <eboot.h>
#include <oal_blmenu.h>
#ifndef EBOOT_NO_LCD
#include <pagemenu.h>
#endif
#include <sermddapi.h>
//------------------------------------------------------------------------------
//
//  Define:  dimof
//
#ifdef dimof
#undef dimof
#endif
#define dimof(x)                (sizeof(x)/sizeof(x[0]))


//------------------------------------------------------------------------------

static VOID ShowSettings(OAL_BLMENU_ITEM *pMenu);
static VOID ShowNetworkSettings(OAL_BLMENU_ITEM *pMenu);
static VOID SetKitlMode(OAL_BLMENU_ITEM *pMenu);
static VOID SaveSettings(OAL_BLMENU_ITEM *pMenu);

//------------------------------------------------------------------------------

extern OAL_BLMENU_ITEM g_menuFlash[];
extern OAL_BLMENU_ITEM g_menuTest[];
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
static void TermInit()
{
	//LCD init and backlight on
	//OMAPLCDInit(TRUE);
	OEMLCDInit(FALSE);
	//SetBacklight(TRUE);
	LCDTermFillRect(0, 0, CX_SCREEN, CY_STATUS_HEIGHT, 0x1F);
}

//------------------------------------------------------------------------------

VOID ShowSettings(OAL_BLMENU_ITEM *pMenu)
{

	SHOW_SETTING_START();

	OALLog(L"\r\n Main:\r\n");
	OALLog(
	    L"  Boot device:   %s\r\n",
	    OALKitlDeviceName(&g_bootCfg.bootDevLoc, g_bootDevices)
	);
	OALLog(
	    L"  Debug device:  %s\r\n",
	    OALKitlDeviceName(&g_bootCfg.kitlDevLoc, g_kitlDevices)
	);
	ShowNetworkSettings(pMenu);
	//SHOW_SETTING_END();
}

//------------------------------------------------------------------------------

VOID ShowNetworkSettings(OAL_BLMENU_ITEM *pMenu)
{

	SHOW_SETTING_START();

	OALLog(L"\r\n Network:\r\n");
	OALLog(
	    L"  KITL state:    %s\r\n",
	    (g_bootCfg.kitlFlags & OAL_KITL_FLAGS_ENABLED) ? L"enabled" : L"disabled"
	);
	OALLog(
	    L"  KITL mode:     %s\r\n",
	    (g_bootCfg.kitlFlags & OAL_KITL_FLAGS_POLL) ? L"poll" : L"interrupt"
	);
	OALLog(
	    L"  DHCP:          %s\r\n",
	    (g_bootCfg.kitlFlags & OAL_KITL_FLAGS_DHCP) ? L"enabled" : L"disabled"
	);
	OALLog(L"  IP address:    %s\r\n", OALKitlIPtoString(g_bootCfg.ipAddress));
	OALLog(L"  IP mask:       %s\r\n", OALKitlIPtoString(g_bootCfg.ipMask));
	OALLog(L"  IP router:     %s\r\n", OALKitlIPtoString(g_bootCfg.ipRoute));
	OALLog(
	    L"  VMINI:         %s\r\n",
	    (g_bootCfg.kitlFlags & OAL_KITL_FLAGS_VMINI) ? L"enabled" : L"disabled"
	);

	SHOW_SETTING_END();

}

//------------------------------------------------------------------------------

VOID SetKitlMode(OAL_BLMENU_ITEM *pMenu)
{
	WCHAR key;

	if ((g_bootCfg.kitlFlags & OAL_KITL_FLAGS_POLL) != 0)
	{
		OALLog(L" Set KITL to interrupt mode [y/-]: ");
	}
	else
	{
		OALLog(L" Set KITL to poll mode [y/-]: ");
	}

	// Get key
	key = OALBLMenuReadKey(TRUE);
	OALLog(L"%c\r\n", key);

	if (key == L'y' || key == L'Y')
	{
		if ((g_bootCfg.kitlFlags & OAL_KITL_FLAGS_POLL) != 0)
		{
			g_bootCfg.kitlFlags &= ~OAL_KITL_FLAGS_POLL;
			OALLog(L" KITL set to interrupt mode\r\n");
		}
		else
		{
			g_bootCfg.kitlFlags |= OAL_KITL_FLAGS_POLL;
			OALLog(L" KITL set to pool mode\r\n");
		}
	}
}

//------------------------------------------------------------------------------

VOID SaveSettings(OAL_BLMENU_ITEM *pMenu)
{
	WCHAR key;

	OALLog(L" Do you want save current settings [-/y]? ");

	// Get key
	key = OALBLMenuReadKey(TRUE);
	OALLog(L"%c\r\n", key);

	// Depending on result
	if (key != L'y' && key != L'Y')
		goto cleanUp;
/*
	if (BLWriteBootCfg(&g_bootCfg))
	{
		OALLog(L" Current settings has been saved\r\n");
	}
	else
	{
		OALLog(L"ERROR: Settings save failed!\r\n");
	}
*/
cleanUp:
	return ;
}


VOID USBdownload(OAL_BLMENU_ITEM *pMenu)
{
	//Visual: for test
	//UCHAR buf[8];
	//OEMUSBInit();
	//while(1)
	//{
	//	OEMUSBReadData(8,buf);
	//	printk(L"%x %x %x %x %x %x %x %x\r\n",
	//		buf[0],buf[1],buf[2],buf[3],
	//		buf[4],buf[5],buf[6],buf[7]);
	//}
	g_bootCfg.bootDevLoc.IfcType = Internal;
	g_bootCfg.bootDevLoc.LogicalLoc = KD_ID_USBSER;
}

//------------------------------------------------------------------------------
static OAL_BLMENU_ITEM g_menuNetwork[] = {
            {
                L'1', L"Show Current Settings", ShowNetworkSettings,
                NULL, NULL, NULL
            }, {
                L'2', L"Enable/disable KITL", OALBLMenuEnable,
                L"KITL", &g_bootCfg.kitlFlags, (VOID*)OAL_KITL_FLAGS_ENABLED
            }, {
                L'3', L"KITL interrupt/poll mode", SetKitlMode,
                NULL, NULL, NULL
            }, {
                L'4', L"Enable/disable DHCP", OALBLMenuEnable,
                L"DHCP", &g_bootCfg.kitlFlags, (VOID*)OAL_KITL_FLAGS_DHCP
            }, {
                L'5', L"Set IP address", OALBLMenuSetIpAddress,
                L"Device", &g_bootCfg.ipAddress, NULL
            }, {
                L'6', L"Set IP mask", OALBLMenuSetIpMask,
                L"Device", &g_bootCfg.ipMask, NULL
            }, {
                L'7', L"Set default router", OALBLMenuSetIpAddress,
                L"Default router", &g_bootCfg.ipRoute, NULL
            }, {
                L'8', L"Enable/disable VMINI", OALBLMenuEnable,
                L"VMINI", &g_bootCfg.kitlFlags, (VOID*)OAL_KITL_FLAGS_VMINI
            }, {
                L'0', L"Exit and Continue", NULL,
                NULL, NULL, NULL
            }, {
                0, NULL, NULL,
                NULL, NULL, NULL
            }
        };

static OAL_BLMENU_ITEM g_menuMain[] = {
                                          {
                                              L'1', L"USB download", USBdownload,
                                              NULL, NULL, NULL
                                          }, 	
#if 0	
                                          {
                                              L'1', L"Show Current Settings", ShowSettings,
                                              NULL, NULL, NULL
                                          },
#endif                                          
                                          {
                                              L'2', L"Select Boot Device", OALBLMenuSelectDevice,
                                              L"Boot", &g_bootCfg.bootDevLoc, g_bootDevices
                                          }, 
#if 0                                          
                                          {
                                              L'3', L"Select Debug Device", OALBLMenuSelectDevice,
                                              L"Debug", &g_bootCfg.kitlDevLoc, g_kitlDevices
                                          }, 
#endif                                         
                                          {
                                              L'4', L"CPU test", OALBLMenuShow,
                                              L"CPU test", &g_menuTest, NULL
                                          },                                          
                                          {
                                              L'5', L"NAND/NOR Flash ", OALBLMenuShow,
                                              L"NAND/NOR Flash", &g_menuFlash, NULL
                                          },                                       
#if 0                                          
                                          {
                                              L'7', L"Save Settings", SaveSettings,
                                              NULL, NULL, NULL
                                          }, 
#endif                                          
                                          {
                                              L'0', L"Exit and Continue", NULL,
                                              NULL, NULL, NULL
                                          }, {
                                              0, NULL, NULL,
                                              NULL, NULL, NULL
                                          }
                                      };

static TCHAR MainHeaderTxt[30]=L"Eboot for S3C2443";
static OAL_BLMENU_ITEM g_menuRoot = {
                                        0, NULL, OALBLMenuShow,
                                        MainHeaderTxt, g_menuMain, NULL
                                    };

//------------------------------------------------------------------------------
VOID BLMenu()
{
#if 0
	UINT32 time, delay = 5;
	WCHAR key = 0;
	BSP_ARGS *pArgs = OALPAtoCA(IMAGE_SHARE_ARGS_PA_START);

	// First let user break to menu
	while (delay > 0 && key != L' ')
	{
		OALLog(L"Hit space to enter configuration menu %d...\r\n", delay);
		time = OALGetTickCount();
		while ((OALGetTickCount() - time) < 1000)
		{
			if ((key = OALBLMenuReadKey(FALSE)) == L' ')
				break;
			if(IsChordKeyPressed(0xc3, 1))
			{
				key=L' ';
				break;
			}
			//press volume_up key to enter this mode
			if(IsChordKeyPressed(KEY_TVOLUMEUP, 2))
			{
				TermInit();
				
				OALLog(L" USB update mode:\r\n");
				OALLog(L"\r\n Please assure \r\n USB cable is connected\r\n");
				g_bootCfg.bootDevLoc.IfcType = Internal;
				g_bootCfg.bootDevLoc.LogicalLoc = 12345+0x10;
				//OMAP730_USBD_REGS_PA
				//start downloading image
				return;
			}
				
		}
		delay--;
	}

	if (key == L' ')
	{
		TermInit();
#endif
		//Visual: update eboot version
		OALLogPrintf(g_menuRoot.pParam1,sizeof(MainHeaderTxt)/sizeof(TCHAR),
		_T("Eboot%d.%d(") _T(__DATE__) _T(")") ,
		EBOOT_VERSION_MAJOR, EBOOT_VERSION_MINOR);
		OEMLCDInit(TRUE);
		OALBLMenuShow(&g_menuRoot);
#if 0
		// We have to invalidate arguments
		pArgs->header.signature = 0;
	}
#endif	
}

